let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        time1: '请选择日期',  //上会时间
        time2: '请选择日期',  //签约时间
        remark: '',  //审核情况
        radio: 2,  //是否入统计库启报投资，1是 2否
        year: '',  //截至xxxx年
        year_val: '',  //截至xxxx年的投资金额
        january_february: '',
        march: '',
        april: '',
        may: '',
        june: '',
        july: '',
        august: '',
        september: '',
        october: '',
        november: '',
        december: '',
        invested: 0,  //已完成投资
        percentage: 0,  //已完成投资占比
        investment: 0,  //总投资
        yearEnd: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options)
        this.setData({ proId: options.proId, yearEnd: new Date().getFullYear() });
        this.init();
    },

    init(){
        wx.showLoading({ title: '加载中' });
        app.request('post', '/api/Project/fixedDetail', {pro_id: this.data.proId}).then(res => {
            this.setData({ invested: res.data.invested, percentage: res.data.percentage, investment: parseFloat(res.data.investment) });
            if(res.data.detail != ''){
                this.setData({
                    time1: res.data.detail.meeting,
                    time2: res.data.detail.signing_time,
                    remark: res.data.detail.remark,
                    radio: res.data.detail.is_ok,
                    year: res.data.detail.year,
                    year_val: res.data.detail.year_val,
                    january_february: res.data.detail.january_february,
                    march: res.data.detail.march,
                    april: res.data.detail.april,
                    may: res.data.detail.may,
                    june: res.data.detail.june,
                    july: res.data.detail.july,
                    august: res.data.detail.august,
                    september: res.data.detail.september,
                    october: res.data.detail.october,
                    november: res.data.detail.november,
                    december: res.data.detail.december
                });
            }
            wx.hideLoading();
        }).catch(err => console.log(err));
    },

    /** 上会时间 */
    changeDate1(e){
        this.setData({ time1: e.detail.value });
    },

    /** 签约时间 */
    changeDate2(e){
        this.setData({ time2: e.detail.value });
    },

    /** 截至年份 */
    changeDate3(e){
        this.setData({ year: e.detail.value });
    },

    /** 审核情况 */
    inputArea(e){
        this.setData({ remark: e.detail.value });
    },

    /** 取消 */
    onCancel(){
        wx.navigateBack();
    },

    /** 是否入统计库启报投资 - 单选按钮 */
    onRadio(e){
        this.setData({ radio: e.currentTarget.dataset.val });
        if(this.data.year == ''){
            if(e.currentTarget.dataset.val == 1) this.setData({ year: new Date().getFullYear() });
        }
    },

    /** 统计库 - 投资金额 输入 */
    onInput(e){
        let key = e.currentTarget.dataset.key;
        let value = isNaN(e.detail.value) ? 0 : e.detail.value;
        this.setData({ [key] : value });
        this.computer();
    },

    /** 计算投资及占比 */
    computer(){
        let arr = [];
        arr.push(this.data.year_val ? parseFloat(this.data.year_val) : 0);
        arr.push(this.data.january_february ? parseFloat(this.data.january_february) : 0);
        arr.push(this.data.march ? parseFloat(this.data.march) : 0);
        arr.push(this.data.april ? parseFloat(this.data.april) : 0);
        arr.push(this.data.may ? parseFloat(this.data.may) : 0);
        arr.push(this.data.june ? parseFloat(this.data.june) : 0);
        arr.push(this.data.july ? parseFloat(this.data.july) : 0);
        arr.push(this.data.august ? parseFloat(this.data.august) : 0);
        arr.push(this.data.september ? parseFloat(this.data.september) : 0);
        arr.push(this.data.october ? parseFloat(this.data.october) : 0);
        arr.push(this.data.november ? parseFloat(this.data.november) : 0);
        arr.push(this.data.december ? parseFloat(this.data.december) : 0);
        let invested = arr.reduce((total,num) => total + num);
        let percentage = (invested / this.data.investment * 100).toFixed(2);
        this.setData({ invested, percentage });
    },

    /** 提交 */
    onSumit(){
        if(this.data.time1 == '请选择日期'){
            app.showFailToast('请选择上会时间');
            return false;
        }
        if(this.data.time2 == '请选择日期'){
            app.showFailToast('请选择签约时间');
            return false;
        }
        if(this.data.remark == ''){
            app.showFailToast('请填写审核情况');
            return false;
        }
        if(this.data.radio == 1){
            if(this.data.year == ''){
                app.showFailToast('请填写截至年份');
                return false;
            }
        }
        let params = {};
        params.pro_id = this.data.proId;
        params.meeting = this.data.time1;
        params.signing_time = this.data.time2;
        params.remark = this.data.remark;
        params.is_ok = this.data.radio;
        if(this.data.radio == 1){
            params.year = this.data.year;
            params.year_val = this.data.year_val;
            params.january_february = this.data.january_february;
            params.march = this.data.march;
            params.april = this.data.april;
            params.may = this.data.may;
            params.june = this.data.june;
            params.july = this.data.july;
            params.august = this.data.august;
            params.september = this.data.september;
            params.october = this.data.october;
            params.november = this.data.november;
            params.december = this.data.december;
        }
        // console.log(params)
        app.request('post', '/api/Project/fixedApi', params).then(res => {
            app.showSuccessToast('提交成功');
            this.init();
        }).catch(err => console.log(err));
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})